<?php
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );

class MySession {
	private $CI;
	function __construct() {
		$this->CI = & get_instance ();
		$this->CI->load->library ( 'session' );
	}
	function login($userid, $name, $role){
		$newdata = array(
				SESSION_KEY_USER_ID 	=> $userid,
				SESSION_KEY_USER_NAME 	=> $name,
				SESSION_KEY_USER_ROLE 	=> $role,
		);
		$this->CI->session->set_userdata($newdata);
	}
	function logout(){
		$userdata = array(
				SESSION_KEY_USER_ID => '',
				SESSION_KEY_USER_NAME => '',
				SESSION_KEY_USER_ROLE => '',
		);
		$this->CI->session->unset_userdata($userdata);
// 		$this->CI->session->sess_destroy();
	}
	/**
	 * 
	 * @return boolean
	 */
	function hasLogin($role= SESSION_KEY_USER_ROLE_USER){
// 		log_message(ETS_INFO, '### '.SESSION_KEY_USER_ID.'='
// 				.$this->CI->session->userdata(SESSION_KEY_USER_ID));
// 		log_message(ETS_INFO, '### '.SESSION_KEY_USER_NAME.'='
// 				.$this->CI->session->userdata(SESSION_KEY_USER_NAME));
// 		log_message(ETS_INFO, '### '.SESSION_KEY_USER_ROLE.'='
// 				.$this->CI->session->userdata(SESSION_KEY_USER_ROLE));
		if(SESSION_KEY_USER_ROLE_ADMIN == $role
			&& SESSION_KEY_USER_ROLE_ADMIN != $this->CI->session->userdata(SESSION_KEY_USER_ROLE)){
			return false;
		}
		return strlen($this->CI->session->userdata(SESSION_KEY_USER_ID)) > 0;
	}
	/**
	 * 
	 * @return boolean
	 */
	function isAdmin(){
		$session_role = $this->CI->session->userdata(SESSION_KEY_USER_ROLE);
		return strlen($session_role)>0 && $session_role == SESSION_KEY_USER_ROLE_ADMIN;
	}
	function setLanguage($language){
		$this->CI->session->set_userdata ( SESSION_KEY_LANGUAGE, $language );
	}
	/**
	 * @return string
	 */
	function getLanguage(){
		return $this->CI->session->userdata ( SESSION_KEY_LANGUAGE );
	}
	
	function getUserName(){
		return $this->CI->session->userdata ( SESSION_KEY_USER_NAME );
	}
	
	/*
	 * @return userID
	 */
	function getUserID(){
		return $this->CI->session->userdata(SESSION_KEY_USER_ID);
	}
	
	
}